package com.hs.hlw.credit.system.common;

import com.hs.hlw.credit.system.common.setting.CookieSetting;
import com.hs.hlw.credit.system.common.setting.SysSetting;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * Created by yangbajing(yangbajing@gmail.com) on 2016-09-9.
 */
@Component
public class WebUtils {

    private final CookieSetting cookieSetting;

    private final SysSetting sysSetting;

    @Autowired
    public WebUtils(CookieSetting cookieSetting, SysSetting sysSetting) {
        this.cookieSetting = cookieSetting;
        this.sysSetting = sysSetting;
    }

    public String getIpFromRequest(HttpServletRequest request) {
        String ip = request.getHeader(CsConstants.X_REAL_IP);
        return ip == null ? request.getRemoteAddr() : ip;
    }

    public String findFromHeader(HttpServletRequest request, String headerString) {
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            if (headerString.equalsIgnoreCase(headerName)) {
                return request.getHeader(headerName);
            }
        }
        return "";
    }


    public Cookie createCookie(String name, String value) {
        Cookie cookie = new Cookie(name, value);
        if (StringUtils.isNoneBlank(cookieSetting.getDomain())) {
            cookie.setDomain(cookieSetting.getDomain());
        }
        if (StringUtils.isNoneBlank(cookieSetting.getPath())) {
            cookie.setPath(cookieSetting.getPath());
        }
        if (cookieSetting.getMaxAge() > 0) {
            cookie.setMaxAge(cookieSetting.getMaxAge());
        }
        cookie.setHttpOnly(cookieSetting.isHttpOnly());
        return cookie;
    }

    public Cookie createCookie(String domain, String name, String value) {
        Cookie cookie = new Cookie(name, value);
        cookie.setDomain(domain);
        if (StringUtils.isNoneBlank(cookieSetting.getPath())) {
            cookie.setPath(cookieSetting.getPath());
        }
        if (cookieSetting.getMaxAge() > 0) {
            cookie.setMaxAge(cookieSetting.getMaxAge());
        }
        cookie.setHttpOnly(cookieSetting.isHttpOnly());
        return cookie;
    }

    public void addCookie(HttpServletResponse response, String name, String value) {
        Cookie domainCookie = createCookie(name, value);
        response.addCookie(domainCookie);
    }

    public void removeCookie(HttpServletRequest request, HttpServletResponse response, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    cookie.setValue("");
                    if (StringUtils.isNoneBlank(cookieSetting.getDomain())) {
                        cookie.setDomain(cookieSetting.getDomain());
                    }
                    if (StringUtils.isNoneBlank(cookieSetting.getPath())) {
                        cookie.setPath(cookieSetting.getPath());
                    }
                    cookie.setHttpOnly(cookieSetting.isHttpOnly());
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }
    }

    /**
     * just return the first result, when exists multiple
     */
    public String getValueByRequest(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

}
